<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 登录表单 -->
      <div class="login-form">
        <div class="title-container">
          <h3 class="title">系统登录</h3>
        </div>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :type="passwordVisible ? 'text' : 'password'"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              @keyup.enter.native="handleLogin">
              <i
                slot="suffix"
                :class="['el-icon-view', passwordVisible ? 'is-active' : '']"
                @click="passwordVisible = !passwordVisible">
              </i>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
            <el-link type="primary" class="forget-pwd" href="javascript:;">忘记密码？</el-link>
          </el-form-item>

          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="login-button"
              @click.native.prevent="handleLogin">
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordVisible: false,
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        await this.$store.dispatch('user/login', this.loginForm)
        this.$router.push({ path: this.redirect || '/' })
      } catch (error) {
        console.error('登录失败:', error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

  .login-box {
    @include responsive('width', 420);
    @include responsive('padding', 35);
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);

    .title-container {
      text-align: center;
      @include responsive('margin-bottom', 30);

      .title {
        @include responsive('font-size', 26);
        color: var(--text-color);
        font-weight: bold;
      }
    }

    .el-input {
      @include responsive('height', 47);

      input {
        @include responsive('height', 47);
        background: transparent;
        border: 1px solid rgba(0, 0, 0, 0.1);
        color: var(--text-color);

        &:-webkit-autofill {
          box-shadow: 0 0 0 1000px #fff inset !important;
        }
      }

      .el-input__prefix {
        @include responsive('font-size', 16);
        color: #909399;
      }
    }

    .el-icon-view {
      color: #909399;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: var(--primary-color);
      }

      &.is-active {
        color: var(--primary-color);
      }
    }

    .forget-pwd {
      float: right;
    }

    .login-button {
      width: 100%;
      @include responsive('height', 44);
      @include responsive('font-size', 16);
      font-weight: bold;
      background: linear-gradient(to right, #667eea, #764ba2);
      border: none;

      &:hover {
        opacity: 0.9;
      }
    }
  }
}

// 暗色主题适配
[data-theme='dark'] .login-container {
  background: linear-gradient(135deg, #434343 0%, #000000 100%);

  .login-box {
    background: rgba(0, 0, 0, 0.8);
  }
}
</style>
